package io.ticktick.modules.app.controller;

import java.util.Arrays;
import java.util.Map;

import io.ticktick.modules.app.annotation.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.ticktick.modules.app.entity.UserEntity;
import io.ticktick.modules.app.service.UserService;
import io.ticktick.common.utils.R;

/**
 * 
 *
 * @author hwf
 * @email zhongku0225@gmail.com
 * @date 2019-11-09 13:17:01
 */
@RestController
@RequestMapping("app/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @Login
    public R list(@RequestParam Map<String, Object> params){

        return R.ok();
    }


    /**
     * 信息
     */
    @Login
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);
        return user != null ? R.ok().put("user", user) : R.ok();
    }

    /**
     * 保存
     */
//    @Login
    @PostMapping("/save")
    public R save(@RequestBody UserEntity user){
        UserEntity userEntity = user;

		userService.save(userEntity);

        return R.ok().put("id", user.getId());
    }

    /**
     * 修改
     */
    @Login
    @PostMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 修改密码
     */
    @PostMapping("/editpassword")
    public R editPassword(@RequestBody UserEntity user){
        UserEntity userEntity = userService.queryById(user.getId());
        if(userEntity == null)
            return R.ok("不存在该用户");
        if(!userEntity.getMobile().equals(user.getMobile()))
            return R.ok("验证失败");

        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @Login
    @GetMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
